package com.kuaimacode.kframework.agent.controller.service;

import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.agent.services.agent.SysAgentService;
import com.kuaimacode.kframework.agent.services.serv.AgentServService;
import com.kuaimacode.kframework.agent.services.service.OneClassService;
import com.kuaimacode.kframework.agent.util.SessionUtil;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.mybatis.models.agent.SysAgent;
import com.kuaimacode.kframework.mybatis.models.category.AgentServ;
import com.kuaimacode.kframework.mybatis.models.service.OneClass;
import com.kuaimacode.kframework.mybatis.models.sys.Dict;
import com.kuaimacode.kframework.service.DictService;
import com.kuaimacode.kframework.util.DictUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @date 2019/03/22
 */

/**
 * Controller
 */
@Controller
@RequestMapping({"/agentService"})
public class AgentServiceController {
	private static String PAGE_FILE_NAME = "agentService/";

	@Autowired
	private AgentServService agentServiceServices;
	@Autowired
	private DictService dictService;

	@Autowired
	private OneClassService oneClassService;

	@Autowired
	private SysAgentService sysAgentService;

	@RequestMapping(value = {"/index/{type}"})
	public String index(@RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM+"") int pageNum,
										@RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE+"") int pageSize,
						@PathVariable int type,ModelMap model) {
		PageInfo<AgentServ> pageInfo = agentServiceServices.findAll(pageNum, pageSize,type);
		// pageInfo.list agentService
		model.put("pageInfo", pageInfo);
		model.put("type", type);
		model.put("title", DictUtil.keyValue("SERVICE_CLASS",type+""));
		return PAGE_FILE_NAME + "index";
	}


	@RequestMapping(value = "/add/{type}", method = RequestMethod.GET)
	public String add(@PathVariable int type, ModelMap map) {
		AgentServ agentService = new AgentServ();
		//查询分站开通的一级分类
		/*List<OneClass> oneClassList = oneClassService.queryOneClass();
		for(OneClass oneClass :oneClassList){
			oneClass.setServiceName(DictUtil.keyValue(Constant.SERVICE_CLASS_DICT_NAME,oneClass.getServiceClass()));
		}*/
        OneClass oneClass = new OneClass();
        oneClass.setServiceClass(type+"");
        oneClass.setServiceName(DictUtil.keyValue(Constant.SERVICE_CLASS_DICT_NAME,oneClass.getServiceClass()));

        //查询二级分类
        Map<String,String> serviceMap =DictUtil.keyValueByPCodeList(Constant.SERVICE_CLASS_DICT_NAME,type+"");

        List<AgentServ> twoClassList = new ArrayList<>();
        List<AgentServ> servList = agentServiceServices.queryAll(type);
        for(AgentServ serv:servList){
            if(serviceMap.containsKey(serv.getServiceCode())){
                serviceMap.remove(serv.getServiceCode());
            }
        }
        for(String key :serviceMap.keySet()){
            AgentServ s = new AgentServ();
            s.setServiceCode(key);
            s.setServiceName(serviceMap.get(key));
            twoClassList.add(s);
        }

		SysAgent agent = sysAgentService.getById(SessionUtil.getAgentId());
		map.put("platRate",agent.getProportion());
		map.put("maxRate",new BigDecimal(100).subtract(agent.getProportion()));


		map.put("agentService", agentService);
		map.put("isAdd", true);
		map.put("oneClass", oneClass);
		map.put("twoClassList", twoClassList);
		return PAGE_FILE_NAME+"form";
	}



	@RequestMapping(value = "/addOneClass", method = RequestMethod.GET)
	public String addOneClass(ModelMap map) {
		Dict dict = new Dict();
		map.put("dict", dict);
		map.put("isAdd", true);
		return PAGE_FILE_NAME+"oneClassform";
	}





	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable String id, ModelMap map) {
		AgentServ agentService = agentServiceServices.getById(id);
		List<OneClass> oneClassList = oneClassService.queryOneClass();
		for(OneClass oneClass :oneClassList){
			oneClass.setServiceName(DictUtil.keyValue(Constant.SERVICE_CLASS_DICT_NAME,oneClass.getServiceClass()));
		}
		map.put("agentService", agentService);
		map.put("isAdd", false);
		map.put("type", agentService.getServiceClass());
		map.put("oneClassList", oneClassList);

		SysAgent agent = sysAgentService.getById(SessionUtil.getAgentId());
		map.put("maxRate",new BigDecimal(100).subtract(agent.getProportion()));
		map.put("platRate",agent.getProportion());
		return PAGE_FILE_NAME+"form";
	}

	@RequestMapping(value = {"/edit"}, method = RequestMethod.POST)
	@ResponseBody
	public JsonResult edit(AgentServ agentService,Boolean isAdd,String updateAll,  ModelMap map) {
		try {
			String retMsg ="";
			if(isAdd){
				// 新建保存
				retMsg = agentServiceServices.saveService(agentService);
			}else{
				// 编辑保存
				retMsg = agentServiceServices.updateService(agentService);
			}
			if(!"success".equals(retMsg)) return JsonResult.failure(retMsg);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}


	@RequestMapping(value = "/changeStat/{id}", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult delete(@PathVariable String id,@RequestParam(value = "status", required = true)String status, ModelMap map) {
		try {
			AgentServ agentServ = agentServiceServices.getById(id);
			agentServ.setDeleteStatus(Integer.parseInt(status));
			agentServiceServices.updateById(agentServ);
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}


	/**
	 * 查询分站未开通的二级分类
	 * @param parentCode
	 * @return
	 */
	@RequestMapping(value = {"/getChildType/{parentCode}"}, method = RequestMethod.GET)
	@ResponseBody
	public JsonResult getChildTypeList(@PathVariable String parentCode) {
		Map<String,String> map =DictUtil.keyValueByPCodeList(Constant.SERVICE_CLASS_DICT_NAME,parentCode);


		List<AgentServ> servList = agentServiceServices.queryAll(Integer.parseInt(parentCode));
		for(AgentServ serv:servList){
			if(map.containsKey(serv.getServiceCode())){
				map.remove(serv.getServiceCode());
			}
		}
		return JsonResult.success(map);
	}


}
